{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "run numpy_struct_array.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('name', 'S32'), ('age', '<i4'), ('weight', '<f4')], align=True)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(b'Zhang', 32, 75.5)\n",
      "{'names':['name','age','weight'], 'formats':['S32','<i4','<f4'], 'offsets':[0,32,36], 'itemsize':40, 'aligned':True}\n"
     ]
    }
   ],
   "source": [
    "print(a[0])\n",
    "print(a[0].dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'Zhang'\n"
     ]
    }
   ],
   "source": [
    "print(a[0]['name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'Li'\n"
     ]
    }
   ],
   "source": [
    "c = a[1]\n",
    "c['name'] = 'Li'\n",
    "print(a[1]['name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[32 24]\n",
      "40\n"
     ]
    }
   ],
   "source": [
    "b = a[\"age\"]\n",
    "print(b)\n",
    "b[0] = 40\n",
    "print(a[0][\"age\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.tofile(\"test.bin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('f1', [('f2', '<i2')])])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype([(\"f1\", [(\"f2\", np.int16)])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('f0', '<i4'), ('f1', '<f8', (2, 3))])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype([(\"f0\", \"i4\"), (\"f1\", \"f8\", (2, 3))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype([('surname', 'S25'), ('age', 'u1')])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dtype({'surname': ('S25', 0), 'age':(np.uint8, 25)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 2.]\n",
      " [6. 8.]]\n",
      "(24, 8)\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[0,1,2], [3,4,5], [6,7,8]], dtype=np.float32)\n",
    "b = a[::2, ::2]\n",
    "print(b)\n",
    "print(b.strides)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 2.],\n",
       "       [6., 8.]], dtype=float32)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 12)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([[0,1,2],[3,4,5],[6,7,8]],dtype=np.float32,order='F')\n",
    "c.strides"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  C_CONTIGUOUS : True\n",
       "  F_CONTIGUOUS : False\n",
       "  OWNDATA : True\n",
       "  WRITEABLE : True\n",
       "  ALIGNED : True\n",
       "  WRITEBACKIFCOPY : False\n",
       "  UPDATEIFCOPY : False"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flags.c_contiguous"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  C_CONTIGUOUS : False\n",
       "  F_CONTIGUOUS : True\n",
       "  OWNDATA : False\n",
       "  WRITEABLE : True\n",
       "  ALIGNED : True\n",
       "  WRITEBACKIFCOPY : False\n",
       "  UPDATEIFCOPY : False"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T.flags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  C_CONTIGUOUS : False\n",
       "  F_CONTIGUOUS : False\n",
       "  OWNDATA : False\n",
       "  WRITEABLE : True\n",
       "  ALIGNED : True\n",
       "  WRITEBACKIFCOPY : False\n",
       "  UPDATEIFCOPY : False"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.flags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "139815953420416\n",
      "139815953927664\n",
      "139815953927664\n"
     ]
    }
   ],
   "source": [
    "print(id(b))\n",
    "print(id(b.base))\n",
    "print(id(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[          0,  4294967296,           1],\n",
       "       [ 4294967296,           1,  8589934592],\n",
       "       [          1,  8589934592,           2],\n",
       "       [ 8589934592,           2, 12884901888]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from numpy.lib.stride_tricks import as_strided\n",
    "a = np.arange(6)\n",
    "b = as_strided(a, shape=(4,3), strides=(4,4))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[          0,  4294967296,           1],\n",
       "       [ 4294967296,           1, 85899345920],\n",
       "       [          1, 85899345920,          20],\n",
       "       [85899345920,          20, 12884901888]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2] = 20\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.00000000e+00,  6.42787610e-01,  9.84807753e-01,  8.66025404e-01,\n",
       "        3.42020143e-01, -3.42020143e-01, -8.66025404e-01, -9.84807753e-01,\n",
       "       -6.42787610e-01, -2.44929360e-16])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "x = np.linspace(0, 2*np.pi, 10)\n",
    "y = np.sin(x)\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.00000000e+00  6.42787610e-01  9.84807753e-01  8.66025404e-01\n",
      "  3.42020143e-01 -3.42020143e-01 -8.66025404e-01 -9.84807753e-01\n",
      " -6.42787610e-01 -2.44929360e-16]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 0.00000000e+00,  6.42787610e-01,  9.84807753e-01,  8.66025404e-01,\n",
       "        3.42020143e-01, -3.42020143e-01, -8.66025404e-01, -9.84807753e-01,\n",
       "       -6.42787610e-01, -2.44929360e-16])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = np.sin(x, out=x)\n",
    "print(x)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(t) == id(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.    0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009]\n",
      "[0.         0.001      0.002      0.003      0.00399999 0.00499998\n",
      " 0.00599996 0.00699994 0.00799991 0.00899988]\n"
     ]
    }
   ],
   "source": [
    "x = [i*0.001 for i in range(1000000)]\n",
    "x = np.array(x)\n",
    "print(x[0:10])\n",
    "np.sin(x,x)\n",
    "print(x[0:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "numpy.float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "print(type(math.sin(0.5)))\n",
    "type(np.sin(0.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.0\n",
      "<class 'float'>\n",
      "<class 'numpy.float64'>\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(6.0).reshape(2,3)\n",
    "print(a.item(1,2))\n",
    "print(type(a.item(1,2)))\n",
    "print(type(a[1,2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3]\n",
      "[1 2 3 4]\n",
      "[1 3 5 7]\n",
      "[1 3 5 7]\n",
      "[1 3 5 7]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(0,4)\n",
    "print(a)\n",
    "b = np.arange(1,5)\n",
    "print(b)\n",
    "print(np.add(a,b))\n",
    "print(np.add(a,b,a))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  7, 17, 31])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([0,1,2,3])\n",
    "x = a * b + c\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  7, 17, 31])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = a*b\n",
    "x += c\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3]) < np.array([3,2,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True False]\n",
      "[ True False  True]\n",
      "[ True False False]\n",
      "[ True  True False]\n",
      "[False False  True]\n",
      "[False  True  True]\n"
     ]
    }
   ],
   "source": [
    "x1 = np.array([1,2,3])\n",
    "x2 = np.array([3,2,1])\n",
    "y = x1 == x2\n",
    "print(y)\n",
    "y = x1 != x2\n",
    "print(y)\n",
    "y = x1 < x2\n",
    "print(y)\n",
    "y = x1 <= x2\n",
    "print(y)\n",
    "y = x1 > x2\n",
    "print(y)\n",
    "y = x1 >= x2\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n",
      "[4 3 2 1 0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([False, False,  True, False, False])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(5)\n",
    "b = np.arange(4,-1,-1)\n",
    "print(a)\n",
    "print(b)\n",
    "a == b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True,  True])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a>b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True,  True,  True])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logical_or(a==b, a>b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-37-4f8cca09e9bd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mb\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0mb\u001b[0m \u001b[0;31m### ValueError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
     ]
    }
   ],
   "source": [
    "a == b and a > b ### ValueError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(a==b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(a==b) and np.any(a>b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True,  True,  True])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(a==b)| (a>b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1, -2, -3, -4, -5])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "~np.arange(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([255, 254, 253, 252, 251], dtype=uint8)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "~np.arange(5, dtype=np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.         0.00500501 0.01001001 0.01501502 0.02002002 0.02502503\n",
      " 0.03003003 0.03503504 0.04004004 0.04504505 0.05005005 0.05505506\n",
      " 0.06006006 0.06506507 0.07007007 0.07507508 0.08008008 0.08508509\n",
      " 0.09009009 0.0950951  0.1001001  0.10510511 0.11011011 0.11511512\n",
      " 0.12012012 0.12512513 0.13013013 0.13513514 0.14014014 0.14514515\n",
      " 0.15015015 0.15515516 0.16016016 0.16516517 0.17017017 0.17517518\n",
      " 0.18018018 0.18518519 0.19019019 0.1951952  0.2002002  0.20520521\n",
      " 0.21021021 0.21521522 0.22022022 0.22522523 0.23023023 0.23523524\n",
      " 0.24024024 0.24524525 0.25025025 0.25525526 0.26026026 0.26526527\n",
      " 0.27027027 0.27527528 0.28028028 0.28528529 0.29029029 0.2952953\n",
      " 0.3003003  0.30530531 0.31031031 0.31531532 0.32032032 0.32532533\n",
      " 0.33033033 0.33533534 0.34034034 0.34534535 0.35035035 0.35535536\n",
      " 0.36036036 0.36536537 0.37037037 0.37537538 0.38038038 0.38538539\n",
      " 0.39039039 0.3953954  0.4004004  0.40540541 0.41041041 0.41541542\n",
      " 0.42042042 0.42542543 0.43043043 0.43543544 0.44044044 0.44544545\n",
      " 0.45045045 0.45545546 0.46046046 0.46546547 0.47047047 0.47547548\n",
      " 0.48048048 0.48548549 0.49049049 0.4954955 ]\n"
     ]
    }
   ],
   "source": [
    "from numpy_frompyfunc import *\n",
    "x = np.linspace(0, 2, 1000)\n",
    "y1 = np.array([triangle_wave(t, 0.6, 0.4, 1.0) for t in x])\n",
    "print(y1[0:100])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.0 0.005005005005005005 0.01001001001001001 0.015015015015015015\n",
      " 0.02002002002002002 0.025025025025025023 0.03003003003003003\n",
      " 0.03503503503503503 0.04004004004004004 0.04504504504504504\n",
      " 0.050050050050050046 0.05505505505505505 0.06006006006006006\n",
      " 0.06506506506506506 0.07007007007007006 0.07507507507507508\n",
      " 0.08008008008008008 0.08508508508508507 0.09009009009009009\n",
      " 0.0950950950950951 0.10010010010010009 0.1051051051051051\n",
      " 0.1101101101101101 0.11511511511511512 0.12012012012012012\n",
      " 0.1251251251251251 0.1301301301301301 0.13513513513513514\n",
      " 0.14014014014014012 0.14514514514514512 0.15015015015015015\n",
      " 0.15515515515515516 0.16016016016016016 0.16516516516516513\n",
      " 0.17017017017017014 0.17517517517517517 0.18018018018018017\n",
      " 0.18518518518518517 0.1901901901901902 0.19519519519519518\n",
      " 0.20020020020020018 0.2052052052052052 0.2102102102102102\n",
      " 0.21521521521521522 0.2202202202202202 0.2252252252252252\n",
      " 0.23023023023023023 0.23523523523523523 0.24024024024024024\n",
      " 0.2452452452452452 0.2502502502502502 0.2552552552552552\n",
      " 0.2602602602602602 0.2652652652652652 0.2702702702702703\n",
      " 0.2752752752752753 0.28028028028028024 0.28528528528528524\n",
      " 0.29029029029029024 0.2952952952952953 0.3003003003003003\n",
      " 0.30530530530530525 0.3103103103103103 0.31531531531531526\n",
      " 0.3203203203203203 0.3253253253253253 0.33033033033033027\n",
      " 0.3353353353353353 0.3403403403403403 0.34534534534534533\n",
      " 0.35035035035035034 0.3553553553553553 0.36036036036036034\n",
      " 0.36536536536536535 0.37037037037037035 0.37537537537537535\n",
      " 0.3803803803803804 0.38538538538538536 0.39039039039039036\n",
      " 0.39539539539539537 0.40040040040040037 0.40540540540540543\n",
      " 0.4104104104104104 0.4154154154154154 0.4204204204204204\n",
      " 0.4254254254254254 0.43043043043043044 0.4354354354354354\n",
      " 0.4404404404404404 0.44544544544544545 0.4504504504504504\n",
      " 0.45545545545545546 0.46046046046046046 0.4654654654654654\n",
      " 0.47047047047047047 0.4754754754754754 0.4804804804804805\n",
      " 0.4854854854854855 0.4904904904904904 0.4954954954954955]\n"
     ]
    }
   ],
   "source": [
    "triangle_ufunc1 = np.frompyfunc(triangle_wave, 4, 1)\n",
    "y2 = triangle_ufunc1(x, 0.6, 0.4, 1.0)\n",
    "print(y2[0:100])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "object\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from numpy_frompyfunc import *\n",
    "print(y2.dtype)\n",
    "y2 = y2.astype(np.float)\n",
    "y2.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "triangle_ufunc2 = np.vectorize(triangle_wave, otypes=[np.float])\n",
    "y3 = triangle_ufunc2(x, 0.6, 0.4, 1.0)\n",
    "print(np.all(y1 == y2))\n",
    "print(np.all(y2 == y3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0]\n",
      " [10]\n",
      " [20]\n",
      " [30]\n",
      " [40]\n",
      " [50]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(6, 1)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(0, 60, 10).reshape(-1, 1)\n",
    "print(a)\n",
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.arange(0, 5)\n",
    "print(b)\n",
    "b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [20, 21, 22, 23, 24],\n",
       "       [30, 31, 32, 33, 34],\n",
       "       [40, 41, 42, 43, 44],\n",
       "       [50, 51, 52, 53, 54]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = a + b\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.shape = 1,5\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = b.repeat(6, axis=0)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0,  0,  0],\n",
       "       [10, 10, 10, 10, 10],\n",
       "       [20, 20, 20, 20, 20],\n",
       "       [30, 30, 30, 30, 30],\n",
       "       [40, 40, 40, 40, 40],\n",
       "       [50, 50, 50, 50, 50]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = a.repeat(5, axis=1)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]\n",
      " [4]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x,y  = np.ogrid[:5,:5]\n",
    "print(x)\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.        ]\n",
      " [0.33333333]\n",
      " [0.66666667]\n",
      " [1.        ]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0. , 0.5, 1. ]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, y = np.ogrid[:1:4j, :1:3j]\n",
    "print(x)\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  4 10]]\n",
      "[[2]\n",
      " [3]\n",
      " [8]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([0,1,4,10])\n",
    "y = np.array([2,3,8])\n",
    "gy, gx =  np.ix_(y, x)\n",
    "print(gx)\n",
    "print(gy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  3,  6, 12],\n",
       "       [ 3,  4,  7, 13],\n",
       "       [ 8,  9, 12, 18]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gx + gy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.add.reduce([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6, 15])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.add.reduce([[1,2,3],[4,5,6]], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 6])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.add.accumulate([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  3,  6],\n",
       "       [ 4,  9, 15]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.add.accumulate([[1,2,3],[4,5,6]], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  3  3  6  4 10]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  6, 10])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3,4])\n",
    "result = np.add.reduceat(a, indices=[0,1,0,2,0,3,0])\n",
    "print(result)\n",
    "result[::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  3,  4],\n",
       "       [ 4,  6,  8],\n",
       "       [ 6,  9, 12],\n",
       "       [ 8, 12, 16],\n",
       "       [10, 15, 20]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply.outer([1,2,3,4,5],[2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[7]\n",
      "[[[[ 0  1  2  3  4]\n",
      "   [ 5  6  7  8  9]\n",
      "   [10 11 12 13 14]\n",
      "   [15 16 17 18 19]]]\n",
      "\n",
      "\n",
      " [[[20 21 22 23 24]\n",
      "   [25 26 27 28 29]\n",
      "   [30 31 32 33 34]\n",
      "   [35 36 37 38 39]]]\n",
      "\n",
      "\n",
      " [[[40 41 42 43 44]\n",
      "   [45 46 47 48 49]\n",
      "   [50 51 52 53 54]\n",
      "   [55 56 57 58 59]]]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/xacrb/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:5: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.\n",
      "  \"\"\"\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(3*4*5).reshape(3,4,5)\n",
    "lidx = [[0],[1],[2]]\n",
    "aidx = np.array(lidx)\n",
    "print(type(aidx))\n",
    "print(a[lidx])\n",
    "print(a[aidx])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7]\n",
      "[[[[ 0  1  2  3  4]\n",
      "   [ 5  6  7  8  9]\n",
      "   [10 11 12 13 14]\n",
      "   [15 16 17 18 19]]]\n",
      "\n",
      "\n",
      " [[[20 21 22 23 24]\n",
      "   [25 26 27 28 29]\n",
      "   [30 31 32 33 34]\n",
      "   [35 36 37 38 39]]]\n",
      "\n",
      "\n",
      " [[[40 41 42 43 44]\n",
      "   [45 46 47 48 49]\n",
      "   [50 51 52 53 54]\n",
      "   [55 56 57 58 59]]]]\n"
     ]
    }
   ],
   "source": [
    "print(a[tuple(lidx)])\n",
    "print(a[aidx,:,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[22 43 22]\n",
      "  [ 2 23  2]]\n",
      "\n",
      " [[27 48 27]\n",
      "  [ 7 28  7]]]\n",
      "(2, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "i0 = np.array([[1,2,1],[0,1,0]])\n",
    "i1 = np.array([[[0]],[[1]]])\n",
    "i2 = np.array([[[2,3,2]]])\n",
    "b = a[i0, i1, i2]\n",
    "print(b)\n",
    "print(b.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1 2 1]\n",
      "  [0 1 0]]\n",
      "\n",
      " [[1 2 1]\n",
      "  [0 1 0]]]\n",
      "[[[0 0 0]\n",
      "  [0 0 0]]\n",
      "\n",
      " [[1 1 1]\n",
      "  [1 1 1]]]\n",
      "[[[2 3 2]\n",
      "  [2 3 2]]\n",
      "\n",
      " [[2 3 2]\n",
      "  [2 3 2]]]\n"
     ]
    }
   ],
   "source": [
    "ind0, ind1, ind2 = np.broadcast_arrays(i0, i1, i2)\n",
    "print(ind0)\n",
    "print(ind1)\n",
    "print(ind2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "2\n",
      "28\n",
      "28\n"
     ]
    }
   ],
   "source": [
    "i,j,k = 0,1,2\n",
    "print(b[i,j,k])\n",
    "print(a[ind0[i,j,k],ind1[i,j,k],ind2[i,j,k]])\n",
    "i,j,k=1,1,1\n",
    "print(b[i,j,k])\n",
    "print(a[ind0[i,j,k],ind1[i,j,k],ind2[i,j,k]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 2, 3)"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = a[1:3, i0, i1]\n",
    "c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 3)"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ind0, ind1 = np.broadcast_arrays(i0, i1)\n",
    "ind0.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[21 41]\n",
      "[21 41]\n"
     ]
    }
   ],
   "source": [
    "i,j,k = 1,1,2\n",
    "print(c[:,i,j,k])\n",
    "print(a[1:3,ind0[i,j,k],ind1[i,j,k]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 2, 3, 4)\n",
      "[ 1  6 11 16]\n",
      "[ 1  6 11 16]\n"
     ]
    }
   ],
   "source": [
    "d = a[i0,:,i1]\n",
    "print(d.shape)\n",
    "i,j,k=1,1,2\n",
    "print(d[i,j,k,:])\n",
    "print(a[ind0[i,j,k],:,ind1[i,j,k]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 7, 3)"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I, J, K, L = 6, 7, 8, 3\n",
    "_, _, v = np.mgrid[:I, :J, :K]\n",
    "idx = np.random.randint(0, K-L, size=(I, J))\n",
    "idx_k = idx.reshape(I,J,1) + np.arange(L)\n",
    "idx_k.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx_i, idx_j, _ = np.ogrid[:I, :J, :K]\n",
    "r = v[idx_i, idx_j, idx_k]\n",
    "i, j = 2, 3\n",
    "r[i, j, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v[i, j, idx[i, j]:idx[i, j] + L]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 2]),)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b1 = np.array([True, False, True, False])\n",
    "np.nonzero(b1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 0, 1]), array([0, 2, 0]))"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b2 = np.array([[True, False, True], [True, False, False]])\n",
    "np.nonzero(b2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(2*3*5).reshape(2,3,5)\n",
    "### a = np.arange(2*3).reshape(2,3)\n",
    "a[b2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[np.nonzero(b2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[15, 17, 20]])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:3, np.nonzero(b2)[0], np.nonzero(b2)[1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
